package web.controller.user;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.EducationService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.EducationRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/7/6.
 * 修改个人教育情况
 */
@RestController
@RequestMapping(Routes.User.EDUCATION)
public class Education extends Base {


    @Autowired
    private EducationService educationService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse save(@RequestBody EducationRequest request) {

        long accountId = getUserIdFromAuthHeader();
        request.setAccountId(accountId);

        ValidationResult result = request.validate();

        if (result.isFailed()) {
            return fail(result.getMessage());

        }


        return educationService.saveEducationInfo(accountId, request.getSchool(), request.getMajor(), request.getGrade());
    }
}
